﻿/*
* (c) by Andreas Kreisl
*
* Sys Monitor - MAIN
* Version: 0.1
*
* License: creative commons BY-NC-SA
*/


// --------------------------------------------------------------------------------
// Static Const's:

var mod_sysmonitor_hOffset = 15; // Border
var mod_sysmonitor_vOffset = 15; // Border
var mod_sysmonitor_width = 200; // inner width of the Widget
var mod_sysmonitor_size = 10; // Referenze Size for all Objects
var mod_sysmonitor_spacing = 1; // Space between every Sensor
var mod_sysmonitor_saveFileName = system.widgetDataFolder + "/SysMonitor.conf";
if(mod_sysmonitor_debug>2) print("* Config Path: " + mod_sysmonitor_saveFileName);


// --------------------------------------------------------------------------------
// Global Variables:

var myPlugins = new Object();
var mySensors = new Array();
var mySensorCount = 0;
var myOrder = new Array();
var myMover = new Array();
var myDelete = new Array();
var myModify = new Array();

var myBackGround;
var myBackGround2;
var myBackGround3;
var myBackGround4;
var myStatsClose;
var myAdd;
var myPref;
var myMoverPosition = -1;
var myMoverTarget = -1;
var myLine;

var myWindowsIsSelected = false;
var lastVisible = -1;
var firstVisible = -1;

var VBSEnabled;
var mod_sysmonitor_WMIExecQuery;
var mod_sysmonitor_path_swap;
var mod_sysmonitor_path_video;
var mod_sysmonitor_path_network;
var mod_sysmonitor_path_process;
var mod_sysmonitor_path_smart;
var mod_sysmonitor_path_smartctl;
var mod_sysmonitor_path_temperature;
var mod_sysmonitor_path_physicaldevice;
var mod_sysmonitor_path_speedfan;

// --------------------------------------------------------------------------------
// Load Global Variables and more:

if(system.platform=="windows") {
	if(mod_sysmonitor_debug>1) print("** Activating WMI");
	var WBEM = COM.createObject( "WbemScripting.SWbemLocator" );
	var wmi = WBEM.ConnectServer( ".", "root/cimv2" );

	mod_sysmonitor_path_smartctl = convertPathToPlatform( widget.extractFile( "Resources/smartctl.exe" ) , true );
	mod_sysmonitor_path_speedfan = convertPathToPlatform( widget.extractFile( "Resources/SpeedFan/SpeedFanConnector.dll" ) , true );
	mod_sysmonitor_path_speedfan = convertPathToPlatform( widget.extractFile( "Resources/SpeedFan/SFInterop.exe.config" ) , true );
	mod_sysmonitor_path_speedfan = convertPathToPlatform( widget.extractFile( "Resources/SpeedFan/SFInterop.exe" ) , true );
	mod_sysmonitor_WMIExecQuery = convertPathToPlatform( widget.extractFile( "Resources/WinWMI/test.vbs" ) , true );
	VBSEnabled = runCommand('cscript.exe //nologo "' + mod_sysmonitor_WMIExecQuery + '"');
	if(VBSEnabled=="OK") {
		VBSEnabled = true;
		mod_sysmonitor_path_swap = convertPathToPlatform( widget.extractFile( "Resources/WinWMI/swap.vbs" ) , true );
		mod_sysmonitor_path_video = convertPathToPlatform( widget.extractFile( "Resources/WinWMI/video.vbs" ) , true );
		mod_sysmonitor_path_network = convertPathToPlatform( widget.extractFile( "Resources/WinWMI/network.vbs" ) , true );
		mod_sysmonitor_path_process = convertPathToPlatform( widget.extractFile( "Resources/WinWMI/process.vbs" ) , true );
		mod_sysmonitor_path_temperature = convertPathToPlatform( widget.extractFile( "Resources/WinWMI/temperature.vbs" ) , true );
		mod_sysmonitor_path_physicaldevice = convertPathToPlatform( widget.extractFile( "Resources/WinWMI/physicaldevice.vbs" ) , true );
		mod_sysmonitor_path_smart = convertPathToPlatform( widget.extractFile( "Resources/WinWMI/smart.vbs" ) , true );
	} else {
		VBSEnabled = false;
		if(mod_sysmonitor_debug>1) print("** Extracting WMIQuery-CLI");
		mod_sysmonitor_WMIExecQuery = convertPathToPlatform( widget.extractFile( "Resources/WMIExecQuery.exe" ) , true );
	}
	if(mod_sysmonitor_debug>1) print("** VBS Enabled: " + VBSEnabled);


}

mod_sysmonitor_GetPlugins();


// --------------------------------------------------------------------------------
// Lets rock ...

function mod_sysmonitor_init() {
	// Define Window Actions
	main.visible = false;
	//main.onMultiClick = mod_sysmonitor_ShowPlugins;
	main.onGainFocus = mod_sysmonitor_ShowMover;
	main.onLoseFocus = mod_sysmonitor_HiddeMover;
	main.onMouseExit = mod_sysmonitor_HiddeMover2;

	if(preferences.mod_sysmonitor_bg_style.value=="default") preferences.mod_sysmonitor_bg_style.value = "lightglass";

	// Create Background
	myBackGround = new class_BackGround(main, (mod_sysmonitor_width + (2 * mod_sysmonitor_hOffset)),2 * mod_sysmonitor_vOffset);

	// Init Anon Stats Window
	myBackGround2 = new class_BackGround(stats, 0,0);
	myBackGround2.level = "topmost";
	myBackGround3 = new Image();
	myBackGround3.window = stats;
	myBackGround4 = new Text();
	myBackGround4.window = stats;
	myBackGround4.hOffset = 120;
	myBackGround4.vOffset = 43;
	myBackGround4.size = 20;
	myBackGround4.data = "Loading ... 0%";
	myBackGround4.color = "#FFFFFF"
	myBackGround4.width = 200;
	myBackGround4.halign = "center";
	myBackGround4.visible = true
	myBackGround2.size(240,10);
	stats.visible = true;
	updateNow();


	myStatsClose = new Image();
	myStatsClose.window = stats;
	myStatsClose.src = "Resources/Buttons/Remove.png";
	myStatsClose.vOffset = 0;
	myStatsClose.hOffset = -100;
	myStatsClose.visible = true;
	myStatsClose.onMouseEnter = function ( ) { this.colorize = "#FF0000"; }
	myStatsClose.onMouseExit = function ( ) { this.colorize = ""; }
	myStatsClose.onClick = function ( ) { stats.visible = false; }

	// The Pref-Button
	myPref = new Image();
	myPref.window = main;
	myPref.src = "Resources/Buttons/Modify.png";
	myPref.hOffset = mod_sysmonitor_hOffset + mod_sysmonitor_width - myPref.width;
	myPref.vOffset = main.height - myPref.height;
	myPref.visible = false;
	myPref.tooltip = getLocalizedString2("mod_sysm_btn_pref");
	myPref.onClick = function ( ) { showWidgetPreferences(); }
	myPref.onMouseEnter = function ( ) { this.colorize = "#0000FF"; }
	myPref.onMouseExit = function ( ) { this.colorize = ""; }

	// The ADD-Button
	myAdd = new Image();
	myAdd.window = main;
	myAdd.src = "Resources/Buttons/Add.png";
	myAdd.hOffset = mod_sysmonitor_hOffset + mod_sysmonitor_width - myAdd.width;
	myAdd.vOffset = 0;
	myAdd.visible = false;
	myAdd.tooltip = getLocalizedString2("mod_sysm_btn_add");
	myAdd.onClick = mod_sysmonitor_ShowPlugins;
	myAdd.onMouseEnter = function ( ) { this.colorize = "#00FF00"; }
	myAdd.onMouseExit = function ( ) { this.colorize = ""; }


	myLine = new Image();
	myLine.window = main;
	myLine.src = "Resources/line.png";
	myLine.hOffset = mod_sysmonitor_hOffset - 3;
	myLine.vOffset = 0;
	myLine.visible = false;

	mod_sysmonitor_PrefChange(1);

	// Load Settings
	if( filesystem.itemExists(mod_sysmonitor_saveFileName) ) {
		if(mod_sysmonitor_debug>2) print("* Settings found, loading!");
		var LoadSettings = filesystem.readFile( mod_sysmonitor_saveFileName , true )

		if(LoadSettings.length>0) {
			if(mod_sysmonitor_debug>2) print("* Creat Backup of loaded Settings.");
			filesystem.copy(mod_sysmonitor_saveFileName , mod_sysmonitor_saveFileName + ".bak" );
		} else {
			if( filesystem.itemExists(mod_sysmonitor_saveFileName + ".bak") ) {
				if(mod_sysmonitor_debug>1) print("** Found backup of Settings, loading!");
				LoadSettings = filesystem.readFile( mod_sysmonitor_saveFileName + ".bak" , true )
				if(!LoadSettings.length>0) {
					if(mod_sysmonitor_debug>0) print("*** Loading Settings: Original and Backup Settings corrupted! Start with Zero Sensors!");
				}
			} else {
				if(mod_sysmonitor_debug>0) print("*** Loading Settings: Original Settings corrupted and Backup Settings missed! Start with Zero Sensors!");
			}
		}

		for(var i = 0; i < LoadSettings.length ; i++) {
			if(LoadSettings[i]=="dummy") LoadSettings[i]="spacer"; // Backward compatibility!
			mod_sysmonitor_CreateNewSensor(LoadSettings[i],JSON.parse( LoadSettings[++i] ));
			myBackGround4.data = "Loading ... " +  Math.round( (i+1)*100/LoadSettings.length ) + "%";
			updateNow();
		}
	} else {
		if(mod_sysmonitor_debug>2) print("* No Settings found! Creating default.");
		mod_sysmonitor_CreateNewSensor("cpu",new Object);
		mod_sysmonitor_CreateNewSensor("spacer",new Object);
		mod_sysmonitor_CreateNewSensor("mem",new Object);
		mod_sysmonitor_CreateNewSensor("swap",new Object);
		mod_sysmonitor_CreateNewSensor("spacer",new Object);
		mod_sysmonitor_CreateNewSensor("hdd",new Object);
	}


	stats.visible = false
	myBackGround4.visible = false
	// Make visible after all Settings are init/loaded
	main.visible = true;

	if(preferences.mod_sysmonitor_anonstat_ask.value=="0") {
		preferences.mod_sysmonitor_anonstat_ask.value="1";
		anon = alert(getLocalizedString2("mod_sysm_anon_firstquestion"),getLocalizedString2("mod_sysm_anon_yes"),getLocalizedString2("mod_sysm_anon_no"));
		if(anon==1) {
			preferences.mod_sysmonitor_anonstat.value = 1;
			savePreferences();
			mod_sysmonitor_GetGUID();
		}
		if(anon==2) {
			preferences.mod_sysmonitor_anonstat.value = 0;
			preferences.mod_sysmonitor_guid.value="";
			savePreferences();
		}
	} else if ((preferences.mod_sysmonitor_anonstat.value=="1")&&(preferences.mod_sysmonitor_guid.value=="")) {
		mod_sysmonitor_GetGUID();
	} else if (preferences.mod_sysmonitor_anonstat.value=="1") {
		mod_sysmonitor_UpdateAnon();
	}

	mod_sysmonitor_HiddeMover();
	print("* " + widget.name + " is up and running. Your GUID: [" + preferences.mod_sysmonitor_guid.value + "]");
	return true;
}

function mod_sysmonitor_SaveSettings() {
	if(preferences.mod_sysmonitor_reset.value==true) {
		if(mod_sysmonitor_debug>1) print("** Reset Settings.");
		preferences.mod_sysmonitor_reset.value=0;
		savePreferences();
		filesystem.remove( mod_sysmonitor_saveFileName );
		filesystem.remove( mod_sysmonitor_saveFileName + ".bak" );
	} else {
		if(mod_sysmonitor_debug>1) print("** Saving Settings.");
		var FileContent = new Array();
		for(var i = 0 ; i < mySensorCount ; i++ ) {
			if(mod_sysmonitor_debug>4) print("Setting ("+i+") "+mySensors[myOrder[i]].plugin);
			FileContent[FileContent.length] = mySensors[myOrder[i]].plugin;
			FileContent[FileContent.length] = JSON.stringify( mySensors[myOrder[i]].settings );
		}
		filesystem.writeFile( mod_sysmonitor_saveFileName, FileContent );
		if(mod_sysmonitor_debug>2) print("* Saved!");
	}
	return true;
}

function mod_sysmonitor_PrefChange() {
	mod_sysmonitor_spacing = parseInt(preferences.mod_sysmonitor_spacing.value);
	mod_sysmonitor_size = parseInt(preferences.mod_sysmonitor_size.value);
	mod_sysmonitor_width = 200 * mod_sysmonitor_size / 10 ;


	myBackGround.style = preferences.mod_sysmonitor_bg_style.value;
	myBackGround2.style = preferences.mod_sysmonitor_bg_style.value;

	myBackGround.opacity(preferences.mod_sysmonitor_bg_opacity.value);
	myBackGround2.opacity(preferences.mod_sysmonitor_bg_opacity.value);
	if(preferences.mod_sysmonitor_bg_colorize.value==1) {
		myBackGround.color(preferences.mod_sysmonitor_bg_color.value);
		myBackGround2.color(preferences.mod_sysmonitor_bg_color.value);
	} else {
		myBackGround.color("");
		myBackGround2.color("");
	}


	myPref.hOffset = mod_sysmonitor_hOffset + mod_sysmonitor_width - myPref.width;
	myPref.vOffset = main.height - myPref.height;

	myAdd.hOffset = mod_sysmonitor_hOffset + mod_sysmonitor_width - myAdd.width;
	myAdd.vOffset = 0;

	myLine.width = mod_sysmonitor_width;


	if(arguments.length==0) {
		if(preferences.mod_sysmonitor_reset.value==true) {
			reloadWidget();
		} else {
			for(var i = 0;i < mySensorCount;i++)
				if(mySensors[myOrder[i]].ReDraw)
					mySensors[myOrder[i]].ReDraw();
			mod_sysmonitor_ReDraw();
		}
		main.focus();
	}

	if(preferences.mod_sysmonitor_anonstat.value=="1" && preferences.mod_sysmonitor_guid.value=="") {
		mod_sysmonitor_GetGUID();
	}

	return true;
}


// --------------------------------------------------------------------------------
// Main Actions

function mod_sysmonitor_ShowMover() {
	myAdd.visible = true;
	myPref.visible = true;
	myWindowsIsSelected = true;

	if(firstVisible>-1) {
		myMover[firstVisible].visible = true;
		myDelete[firstVisible].visible = true;
		myModify[firstVisible].visible = true;
		lastVisible = firstVisible;
		firstVisible = -1;
	}
	return true;
}

function mod_sysmonitor_HiddeMover() {
	myAdd.visible = false;
	myPref.visible = false;
	for(var i = 0;i < mySensorCount;i++) {
		myMover[i].visible = false;
		myDelete[i].visible = false;
		myModify[i].visible = false;
	}
	myWindowsIsSelected = false;
	lastVisible = -1;
	firstVisible = -1;
		myMoverTarget = -1;
		myMoverPosition = -1;
		myLine.visible = false;
	return true;
}

function mod_sysmonitor_HiddeMover2() {
	for(var i = 0;i < mySensorCount;i++) {
		myMover[i].visible = false;
		myDelete[i].visible = false;
		myModify[i].visible = false;
	}
	lastVisible = -1;
	return true;
}

function mod_sysmonitor_ShowMoverOnMouseEnter() {
	if(myWindowsIsSelected && myMoverPosition==-1) {
		if(lastVisible>-1) {
			myMover[lastVisible].visible = false;
			myDelete[lastVisible].visible = false;
			myModify[lastVisible].visible = false;
		}
		myMover[this.OrderID].visible = true;
		myDelete[this.OrderID].visible = true;
		myModify[this.OrderID].visible = true;
		lastVisible = this.OrderID;
	} else {
		firstVisible = this.OrderID;
	}

	if(myMoverPosition>-1) {
		if(this.OrderID<myMoverPosition) {
			myLine.vOffset = this.vOffset - (mod_sysmonitor_spacing * (mod_sysmonitor_size / 10) / 2) - (myLine.height / 2);
			myLine.visible = true;
			myMoverTarget = this.OrderID;
			myLine.orderAbove(this);
		} else if(this.OrderID>myMoverPosition) {
			myLine.vOffset = this.vOffset + this.height + (mod_sysmonitor_spacing * (mod_sysmonitor_size / 10) / 2)  - (myLine.height / 2);
			myLine.visible = true;
			myMoverTarget = this.OrderID;
			myLine.orderAbove(this);
		}
	}

	return true;
}

function mod_sysmonitor_HiddeMoverOnMouseExit() {
	myMoverTarget = -1;
	myLine.visible = false;
	return true;
}


function mod_sysmonitor_ShowPlugins() {
	var x_menu = new Array();
	for(var i in myPlugins) {
		x_menu[x_menu.length] = new MenuItem();
		x_menu[x_menu.length-1].title = myPlugins[i].name;

		if(myPlugins[i]["onlyonce"])
			for(var j = 0;j < mySensorCount;j++)
				if(i==mySensors[myOrder[j]].plugin)
					x_menu[x_menu.length-1].enabled = false;

		x_menu[x_menu.length-1].myPluginID = i;
		x_menu[x_menu.length-1].onSelect = function ( ) { mod_sysmonitor_CreateNewSensor(this.myPluginID,new Object()); }
	}
	popupMenu(x_menu, system.event.hOffset, system.event.vOffset );
	return true;
}

function mod_sysmonitor_CreateNewSensor(PluginIDtoGenerate,NewSettings) {
	var newSensoreID = mySensors.length
	if(mod_sysmonitor_debug>2) print("Adding Plugin: " + PluginIDtoGenerate + " Order ID: " + (mySensorCount - 0) + " Next Sensor ID: " + newSensoreID + " Sensor Count: " + (mySensorCount + 1));

	mySensors[newSensoreID] = myPlugins[PluginIDtoGenerate].newObject(NewSettings);

	if(mySensors[newSensoreID].canceledplugin) {
		if(mod_sysmonitor_debug>1) print("** Adding Plugin: Canceled!");
		delete mySensors[newSensoreID];
		return true;
	}

	if(mod_sysmonitor_debug>3) print("* Adding Plugin: Successfull!");
	mySensorCount++;
	myOrder[mySensorCount - 1] = newSensoreID;

	mySensors[newSensoreID].frame.hOffset = mod_sysmonitor_hOffset;
	if((mySensorCount - 1)==0) {
		mySensors[newSensoreID].frame.vOffset = mod_sysmonitor_vOffset + (mod_sysmonitor_spacing * (mod_sysmonitor_size / 10) / 2);
	}else {
		mySensors[newSensoreID].frame.vOffset = mySensors[myOrder[mySensorCount - 2]].frame.vOffset + mySensors[myOrder[mySensorCount - 2]].frame.height + mod_sysmonitor_spacing * (mod_sysmonitor_size / 10);
	}
	mySensors[newSensoreID].frame.SensoreID = newSensoreID;
	mySensors[newSensoreID].frame.onMouseEnter = mod_sysmonitor_ShowMoverOnMouseEnter;
	mySensors[newSensoreID].frame.onMouseExit = mod_sysmonitor_HiddeMoverOnMouseExit;


	if(!myMover[mySensorCount - 1]) {
	myMover[mySensorCount - 1] = new Image();
	myMover[mySensorCount - 1].window = main;
	myMover[mySensorCount - 1].src = "Resources/Buttons/Move.png";
	myMover[mySensorCount - 1].vOffset = mySensors[newSensoreID].frame.vOffset + mySensors[newSensoreID].frame.height / 2 - myMover[mySensorCount - 1].height / 2 + 1;
	//myMover[mySensorCount - 1].hOffset = mod_sysmonitor_hOffset - myMover[mySensorCount - 1].width;
	myMover[mySensorCount - 1].hOffset = 0;
	myMover[mySensorCount - 1].visible = false;
	myMover[mySensorCount - 1].id = mySensorCount - 1;
	myMover[mySensorCount - 1].tooltip = widget.getLocalizedString("mod_sysm_btn_move");
	myMover[mySensorCount - 1].onMouseDown = myMover_onMouseDown;
	myMover[mySensorCount - 1].onMouseUp   = myMover_onMouseUp;
	myMover[mySensorCount - 1].onMouseEnter = function ( ) { this.colorize = "#FFFF00"; }
	myMover[mySensorCount - 1].onMouseExit = function ( ) { if(myMoverPosition==-1) this.colorize = ""; }
	} else myMover[mySensorCount - 1].visible = false;

	if(!myDelete[mySensorCount - 1]) {
	myDelete[mySensorCount - 1] = new Image();
	myDelete[mySensorCount - 1].window = main;
	myDelete[mySensorCount - 1].src = "Resources/Buttons/Remove.png";
	myDelete[mySensorCount - 1].vOffset = mySensors[newSensoreID].frame.vOffset + mySensors[newSensoreID].frame.height / 2 - myDelete[mySensorCount - 1].height / 2 + 1;
	myDelete[mySensorCount - 1].hOffset = mySensors[newSensoreID].frame.hOffset + mySensors[newSensoreID].frame.width;
	myDelete[mySensorCount - 1].visible = false;
	myDelete[mySensorCount - 1].id = parseInt(mySensorCount) - 1;
	myDelete[mySensorCount - 1].tooltip = widget.getLocalizedString("mod_sysm_btn_delete");
	myDelete[mySensorCount - 1].onMouseEnter = function ( ) { this.colorize = "#FF0000"; }
	myDelete[mySensorCount - 1].onMouseExit = function ( ) { this.colorize = ""; }
	myDelete[mySensorCount - 1].onMultiClick = myDelete_onMouseClick;
	} else myDelete[mySensorCount - 1].visible = false;

	if(!myModify[mySensorCount - 1]) {
	myModify[mySensorCount - 1] = new Image();
	myModify[mySensorCount - 1].window = main;
	myModify[mySensorCount - 1].src = "Resources/Buttons/Modify.png";
	myModify[mySensorCount - 1].vOffset = mySensors[newSensoreID].frame.vOffset + mySensors[newSensoreID].frame.height / 2 - myModify[mySensorCount - 1].height / 2 + 1;
	myModify[mySensorCount - 1].hOffset = mySensors[newSensoreID].frame.hOffset + mySensors[newSensoreID].frame.width - myModify[mySensorCount - 1].width;
	myModify[mySensorCount - 1].visible = false;
	myModify[mySensorCount - 1].id = mySensorCount - 1;
	myModify[mySensorCount - 1].tooltip = widget.getLocalizedString("mod_sysm_btn_modify");
	myModify[mySensorCount - 1].onMouseEnter = function ( ) { this.colorize = "#0000FF"; }
	myModify[mySensorCount - 1].onMouseExit = function ( ) { this.colorize = ""; }
	myModify[mySensorCount - 1].onClick = myModify_onMouseClick;
	myModify[mySensorCount - 1].onMultiClick = myModify_onMouseClick;
	} else myModify[mySensorCount - 1].visible = false;

	mod_sysmonitor_ReDraw();
	return newSensoreID;
}

function myDelete_onMouseClick() {
	mySensorCount--;
	if(mod_sysmonitor_debug>1) print("Removing Plugin: " + mySensors[myOrder[this.id]].plugin + " Order " + this.id + " Sensor ID: " + myOrder[this.id] + " Sensor Count: " + mySensorCount);

	myMover[mySensorCount].visible = false;
	myDelete[mySensorCount].visible = false;
	myModify[mySensorCount].visible = false;
	if(this.id == mySensorCount) lastVisible = -1;


	mySensors[myOrder[this.id]].frame.visible = false;
	if(mySensors[myOrder[this.id]].Delete) mySensors[myOrder[this.id]].Delete();
	else delete mySensors[myOrder[this.id]];

	if(parseInt(this.id) < mySensorCount)
	for(var i = parseInt(this.id);i < mySensorCount;i++){
		myOrder[i] = myOrder[i+1];
	}

	delete myMover[mySensorCount];
	delete myDelete[mySensorCount];
	delete myModify[mySensorCount];

	mod_sysmonitor_ReDraw()
	return true;
}

function myModify_onMouseClick() {
	mySensors[myOrder[this.id]].edit();
	main.focus();
	return true;
}


// --------------------------------------------------------------------------------
// Re-Order Sensors

function myMover_onMouseDown() {
	myMoverPosition = this.id;
	this.colorize = "#00FF00";
	return true;
}

function myMover_onMouseUp() {
	if(myMoverPosition>-1) {
		myMover[myMoverPosition].colorize = "";
		if(myMoverPosition > -1 && myMoverTarget > -1) {
			// ReDraw the Buttons on the correct Postion (mouse got moved)
			if(lastVisible>-1) {
				myMover[lastVisible].visible = false;
				myDelete[lastVisible].visible = false;
				myModify[lastVisible].visible = false;
			}
			myMover[myMoverTarget].visible = true;
			myDelete[myMoverTarget].visible = true;
			myModify[myMoverTarget].visible = true;
			lastVisible = myMoverTarget;

			// Now move the Sensore
			if(mod_sysmonitor_debug>3) print("Moving: " + myMoverPosition + " to " + myMoverTarget + " ("+mySensors[myOrder[myMoverPosition]].plugin+"/"+mySensors[myOrder[myMoverTarget]].plugin+")");
			if(mod_sysmonitor_debug>3) print("Numeric Order / Sensore ID");
			myMover[myMoverTarget].colorize = "";
			if(parseInt(myMoverTarget) > parseInt(myMoverPosition)) {
				var tmp = myOrder[myMoverPosition];
				for(var i = parseInt(myMoverPosition); i < parseInt(myMoverTarget) ; i++) {
					if(mod_sysmonitor_debug>3) print(i + " <- " + (i+1) + " / " + myOrder[i] + " <- " + myOrder[(i+1)]);
					myOrder[i] = myOrder[(i+1)];
				}
				myOrder[myMoverTarget] = tmp;
			} else {
				var tmp = myOrder[myMoverPosition];
				for(var i = parseInt(myMoverPosition); i > parseInt(myMoverTarget); i--) {
					if(mod_sysmonitor_debug>3) print(i + " <- " + (i-1) + " / " + myOrder[i] + " <- " + myOrder[(i-1)]);
					myOrder[i] = myOrder[(i-1)];
				}
				myOrder[myMoverTarget] = tmp;
			}
			if(mod_sysmonitor_debug>3) print("Move Done!");
			mod_sysmonitor_ReDraw();
		}
		myMoverTarget = -1;
		myMoverPosition = -1;
		myLine.visible = false;
	}
	return true;
}

// --------------------------------------------------------------------------------
// General Functions

function mod_sysmonitor_GetPlugins() {
	include("Plugins/spacer.js");

	include("Plugins/cpu.js");
	include("Plugins/mem.js");
	include("Plugins/swap.js");
	include("Plugins/hdd.js");
	include("Plugins/smart.js");

	include("Plugins/wireless.js");
	include("Plugins/network.js");
	include("Plugins/wan.js");

	include("Plugins/recyclebin.js");
	include("Plugins/battery.js");
	include("Plugins/volumecontrol.js");

	include("Plugins/uptime.js");
	include("Plugins/videocontroller.js");

	include("Plugins/process.js");
	include("Plugins/temperature.js");

	include("Plugins/version.js");

	//include("Plugins/PHP_host.js");
	//include("Plugins/PHP_mem.js");
	//include("Plugins/PHP_uptime.js");

/* Not very secure so it is disabled
	if(!filesystem.itemExists( system.widgetDataFolder + "/Plugins" )) filesystem.createDirectory( system.widgetDataFolder + "/Plugins" );
	var fileList = filesystem.getDirectoryContents(system.widgetDataFolder + "/Plugins", false);
	for (i in fileList) {
		if(fileList[i].substr(-3)==".js") {
			if(mod_sysmonitor_debug>2) print("Debug - mod_sysmonitor: Including " + i + " = " + fileList[i]);
			include(system.widgetDataFolder + "/Plugins/" + fileList[i]);
		} else if(mod_sysmonitor_debug>3) print("Debug - mod_sysmonitor: NOT Included " + i + " = " + fileList[i]);
	}
*/
	return true;
}

function mod_sysmonitor_ReDraw() {
	if(mod_sysmonitor_debug>3) print("mod_sysmonitor_ReDraw");
	for(var i = 0;i < mySensorCount;i++) {
		//if(mod_sysmonitor_debug>3) print("mod_sysmonitor_ReDraw: " + i);
		if(i==0) {
			mySensors[myOrder[0]].frame.vOffset = mod_sysmonitor_vOffset + (mod_sysmonitor_spacing * (mod_sysmonitor_size / 10) / 2);
		} else {
			mySensors[myOrder[i]].frame.vOffset = mySensors[myOrder[i-1]].frame.vOffset + mySensors[myOrder[i-1]].frame.height + mod_sysmonitor_spacing * (mod_sysmonitor_size / 10);
		}

		//myMover[i].hOffset = mod_sysmonitor_hOffset - myMover[i].width + (i % parseInt(preferences.mod_sysmonitor_mover_count.value)) * (myMover[i].width * parseInt(preferences.mod_sysmonitor_mover_size.value) / 10);
		myMover[i].vOffset  = mySensors[myOrder[i]].frame.vOffset + mySensors[myOrder[i]].frame.height / 2 - myMover[i].height  / 2 + 1;
		myMover[i].orderAbove(mySensors[myOrder[i]].frame);
		myDelete[i].vOffset = mySensors[myOrder[i]].frame.vOffset + mySensors[myOrder[i]].frame.height / 2 - myDelete[i].height / 2 + 1;
		myDelete[i].hOffset = mySensors[myOrder[i]].frame.hOffset + mySensors[myOrder[i]].frame.width;
		myDelete[i].orderAbove(mySensors[myOrder[i]].frame);
		myModify[i].vOffset = mySensors[myOrder[i]].frame.vOffset + mySensors[myOrder[i]].frame.height / 2 - myModify[i].height / 2 + 1;
		myModify[i].hOffset = mySensors[myOrder[i]].frame.hOffset + mySensors[myOrder[i]].frame.width - myModify[i].width;
		myModify[i].orderAbove(mySensors[myOrder[i]].frame);



		mySensors[myOrder[i]].frame.OrderID = i;
	}

	//if(mod_sysmonitor_debug>3) print("mod_sysmonitor_ReDraw: ReSize");
	if(mySensorCount>0) {
		myBackGround.size((mod_sysmonitor_width + (2 * mod_sysmonitor_hOffset)),mySensors[myOrder[mySensorCount-1]].frame.vOffset + mySensors[myOrder[mySensorCount-1]].frame.height + mod_sysmonitor_vOffset + (mod_sysmonitor_spacing * (mod_sysmonitor_size / 10) / 2) + 3);
	} else {
		myBackGround.size((mod_sysmonitor_width + (2 * mod_sysmonitor_hOffset)),0);
	}
	myPref.vOffset = main.height - myPref.height;

	if(mod_sysmonitor_debug>3) print("mod_sysmonitor_ReDraw: Done");
	return true;
}

function mod_sysmonitor_ScreenShot() {
	var tmp = saveAs(".png");
	if(tmp) {
		mod_sysmonitor_HiddeMover();
		//main.root.saveImageToFile( system.userDesktopFolder + "/"+widget.name+".png", "png" );
		main.root.saveImageToFile( tmp , "png" );
		//mod_sysmonitor_ShowMover();
	}
	return true;
}

var mod_sysmonitor_GetGUID_once = false;
function mod_sysmonitor_GetGUID() {
	if(!mod_sysmonitor_GetGUID_once) {
		mod_sysmonitor_GetGUID_once = true;
		var url = new URL()
		url.location = "http://tools.bluepaw.de/widgets/guid.php?guid=" + preferences.mod_sysmonitor_guid.value + "&sys=" + widget.name + "&ver=" + widget.version + "&os=" + system.platform;
		if(mod_sysmonitor_debug>2) print("* GetGUID: " + url.location);
		url.fetchAsync(mod_sysmonitor_GetGUID_done);
	}
	return true;
}

function mod_sysmonitor_GetGUID_done( url ) {
	if(url.response == 200) {
		preferences.mod_sysmonitor_guid.value = url.result;
		savePreferences();
	}
	if(mod_sysmonitor_debug>2) print("* Got GUID [" + url.response + "][" + url.result + "][" + url.location + "]");
	mod_sysmonitor_GetGUID_once = false;
	return true;
}

function mod_sysmonitor_UpdateAnon() {
	var url = new URL()
	var tmpPList = preferences.mod_sysmonitor_size.value + ";" + preferences.mod_sysmonitor_spacing.value + ";" + preferences.mod_sysmonitor_bg_style.value + ";" + preferences.mod_sysmonitor_bar_style.value + ";";


	for(var i in mySensors)
		tmpPList += ";" + mySensors[i].plugin;

	url.location = "http://tools.bluepaw.de/widgets/anon.php?guid=" + preferences.mod_sysmonitor_guid.value + "&ver=" + widget.version + "&plugins=" + tmpPList;
	if(mod_sysmonitor_debug>2) print("* Anon Statistics: " + url.location);
	url.fetchAsync(mod_sysmonitor_UpdateAnon_done);
	return true;
}

function mod_sysmonitor_UpdateAnon_done( url ) {
	return true;
	if(url.response == 200) {
		if(mod_sysmonitor_debug>2) print("* Anon Statistics updated.");
	} else if(mod_sysmonitor_debug>0) print("*** Anon Statistics error!");
}

function mod_sysmonitor_AnonStats() {
	if (preferences.mod_sysmonitor_anonstat.value=="1" && preferences.mod_sysmonitor_guid.value!="") {
		myBackGround3.hOffset = 20;
		myBackGround3.vOffset = 20;
		myBackGround3.src = "";
		myBackGround3.src = "http://www.bluepaw.de/projects/widgets/SysMonitorStats.png";

		myBackGround2.size(40 + myBackGround3.width, 40 + myBackGround3.height);

		myStatsClose.hOffset = myBackGround3.width - myStatsClose.width;


		stats.visible = true;
	} else {
		alert(widget.getLocalizedString("mod_sysm_anon_result"));
	}
}

function getLocalizedString2(s) {
    return widget.getLocalizedString(s);
}

// --------------------------------------------------------------------------------
// The END.